package com.swt.draw.config.shiro;

import com.swt.draw.dao.AdminMapper;
import com.swt.draw.entity.Admin;
import com.swt.draw.entity.Limit;
import com.swt.draw.example.AdminExample;
import com.swt.draw.vo.AdminDetailVO;
import com.swt.draw.vo.RoleDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Slf4j
public class MyRealm extends AuthorizingRealm {

    @Resource
    private AdminMapper adminMapper;

    public MyRealm() {
        setName("MyRealm");
    }

    /**
     * 授权
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        // 获取登录用户
        Admin admin = (Admin) SecurityUtils.getSubject().getPrincipal();
        AdminDetailVO detail = adminMapper.getAdminDetailInfoByNo(admin.getAdminNo());
        Set<String> roles = new HashSet<>();
        Set<String> limits = new HashSet<>();
        List<RoleDetailVO> roleDetails = detail.getRoleDetails();
        for(RoleDetailVO detailVO:roleDetails){
            roles.add(detailVO.getRoleNo());
            List<Limit> limitList = detailVO.getLimits();
            for(Limit limit:limitList){
                limits.add(limit.getLimitNo());
            }
        }
        simpleAuthorizationInfo.setRoles(roles);
        simpleAuthorizationInfo.setStringPermissions(limits);
        return simpleAuthorizationInfo;
    }

    /**
     * 认证
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken)
            throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
        String loginname = token.getPrincipal().toString(); // 获取登录用户名
        if (StringUtils.isEmpty(loginname)) {
            return null;
        }
        // 根据用户名从数据库查询用户
        AdminExample example = new AdminExample();
        AdminExample.Criteria criteria = example.createCriteria();
        criteria.andAdminLoginNameEqualTo(loginname);
        criteria.andAdminStatusNotEqualTo((short) 2);
        List<Admin> admins = adminMapper.selectByExample(example);
        if(admins == null || admins.size() != 1){
            return null;
        }
        Admin admin = admins.get(0);
        /*Admin admin = new Admin();
        admin.setAdminPwd("928bfd2577490322a6e19b793691467e");
        admin.setAdminLoginName("admin");*/
        return new SimpleAuthenticationInfo(admin, admin.getAdminPwd(), ByteSource.Util.bytes(admin.getAdminLoginName()), getName());
    }

}
